package requests

import (
	"gohub/app/http/requests/validators"

	"github.com/gin-gonic/gin"
	"github.com/thedevsaddam/govalidator"
)

type VerifyCodePhoneRequest struct {
	CaptchaID     string `json:"captcha_id,omitempty" valid:"captcha_id"`
	CaptchaAnswer string `json:"captcha_answer,omitempty" valid:"captcha_answer"`
	Phone         string `json:"phone,omitempty", valid:"phone"`
}

type VerifyCodeEmailRequest struct {
	CaptchaID     string `json:"captcha_id,omitempty" valid:"captcha_id"`
	CaptchaAnswer string `json:"captcha_answer,omitempty" valid:"captcha_answer"`
	Email         string `json:"email,omitempty" valid:"email"`
}

func VerifyCodePhone(data interface{}, c *gin.Context) map[string][]string {
	errs := validate(data,
		// 校验规则
		govalidator.MapData{
			"phone":          []string{"required", "digits:11"},
			"captcha_id":     []string{"required"},
			"captcha_answer": []string{"required", "digits:6"},
		},
		// 校验错误提示信息
		govalidator.MapData{
			"phone": []string{
				"required:手机号为必填项，参数名称 phone",
				"digits:手机号长度必须为 11 位的数字",
			},
			"captcha_id": []string{
				"required:图片验证码的 ID 为必填",
			},
			"captcha_answer": []string{
				"required:图片验证码答案必填",
				"digits:图片验证码长度必须为 6 位的数字",
			},
		},
	)

	//校验
	_data := data.(*VerifyCodePhoneRequest)
	errs = validators.ValidateCaptcha(_data.CaptchaID, _data.CaptchaAnswer, errs)

	return errs
}

func VerifyCodeEmail(data interface{}, c *gin.Context) map[string][]string {
	errs := validate(data,
		// 校验规则
		govalidator.MapData{
			"email":          []string{"required", "min:4", "max:30", "email"},
			"captcha_id":     []string{"required"},
			"captcha_answer": []string{"required", "digits:6"},
		},
		// 校验错误提示信息
		govalidator.MapData{
			"email": []string{
				"required:Email 为必填项",
				"min:Email 长度需大于 4",
				"max:Email 长度需小于 30",
				"email:Email 格式不正确，请提供有效的邮箱地址",
			},
			"captcha_id": []string{
				"required:图片验证码的 ID 为必填",
			},
			"captcha_answer": []string{
				"required:图片验证码答案必填",
				"digits:图片验证码长度必须为 6 位的数字",
			},
		},
	)

	// 图片验证码
	_data := data.(*VerifyCodeEmailRequest)
	errs = validators.ValidateCaptcha(_data.CaptchaID, _data.CaptchaAnswer, errs)

	return errs
}
